package com.asa.voice.mp3.asa.instream;

import java.io.IOException;
import java.io.RandomAccessFile;

public class MultiplexAudio extends RandomRead {
	  private static final int BUF_LEN = 1024;
	  
	  private byte[] buf;
	  
	  private int curPos;
	  
	  private int mpegVersion;
	  
	  private int packet_data_bytes;
	  
	  private RandomAccessFile raf;
	  
	  public boolean open(String paramString1, String paramString2) throws IOException {
	    this.buf = new byte[1024];
	    this.raf = new RandomAccessFile(paramString1, "r");
	    if (this.raf.read(this.buf) < 1024)
	      return false; 
	    this.curPos = 0;
	    if (!audio_stream())
	      return false; 
	    return true;
	  }
	  
	  private boolean needBytes(int paramInt) throws IOException {
	    int i = 1024 - this.curPos;
	    if (i < paramInt) {
	      System.arraycopy(this.buf, this.curPos, this.buf, 0, i);
	      if (this.raf.read(this.buf, i, this.curPos) < this.curPos)
	        return false; 
	      this.curPos = 0;
	    } 
	    return true;
	  }
	  
	  private boolean skip(int paramInt) throws IOException {
	    int i = 1024 - this.curPos;
	    if (paramInt < i) {
	      this.curPos += paramInt;
	      needBytes(6);
	    } else {
	      paramInt -= i;
	      this.raf.seek(paramInt + this.raf.getFilePointer());
	      this.raf.read(this.buf);
	      this.curPos = 0;
	    } 
	    return true;
	  }
	  
	  private boolean start_code() throws IOException {
	    byte b = 0;
	    while (this.buf[this.curPos] != 0 || this.buf[this.curPos + 1] != 0 || this.buf[this.curPos + 2] != 1) {
	      if (++b > 524288)
	        return false; 
	      this.curPos++;
	      if (1024 - this.curPos < 6)
	        needBytes(6); 
	    } 
	    return true;
	  }
	  
	  private void pack() throws IOException {
	    if ((this.buf[this.curPos + 4] & 0xF0) == 32) {
	      if (!needBytes(12))
	        return; 
	      if (this.duration == 0) {
	        int i = (this.buf[this.curPos + 9] & Byte.MAX_VALUE) << 16;
	        i |= (this.buf[this.curPos + 10] & 0xFF) << 8;
	        i |= this.buf[this.curPos + 11] & 0xFF;
	        i >>= 1;
	        this.duration = (int)(this.raf.length() / i / 50L);
	        System.out.printf("duration: %ds\n", new Object[] { Integer.valueOf(this.duration) });
	      } 
	      this.mpegVersion = 1;
	      skip(12);
	    } else if ((this.buf[this.curPos + 4] & 0xF0) == 64) {
	      if (!needBytes(14))
	        return; 
	      this.mpegVersion = 2;
	      skip(14 + (this.buf[this.curPos + 13] & 0x7));
	    } 
	  }
	  
	  private boolean audio_stream() throws IOException {
	    while (true) {
	      int k;
	      if (!start_code())
	        return false; 
	      int i;
	      if ((i = this.buf[this.curPos + 3] & 0xFF) < 185) {
	        this.curPos += 4;
	        continue;
	      } 
	      switch (i) {
	        case 185:
	          return false;
	        case 186:
	          pack();
	          continue;
	        case 189:
	          if (this.mpegVersion == 1)
	            continue; 
	        case 192:
	          int j = (this.buf[this.curPos + 4] & 0xFF) << 8 | this.buf[this.curPos + 5] & 0xFF;
	          k = 6;
	          if ((this.buf[this.curPos + 6] & 0xC0) == 128) {
	            k += 3 + (this.buf[this.curPos + 8] & 0xFF);
	          } else {
	            while ((this.buf[this.curPos + k] & 0xFF) == 255)
	              k++; 
	            if ((this.buf[this.curPos + k] & 0xC0) == 64)
	              k += 2; 
	            if ((this.buf[this.curPos + k] & 0xF0) == 32) {
	              k += 5;
	            } else if ((this.buf[this.curPos + k] & 0xF0) == 48) {
	              k += 10;
	            } else if (this.buf[this.curPos + k] == 15) {
	              k++;
	            } 
	          } 
	          this.packet_data_bytes = j - k + 6;
	          if (this.packet_data_bytes > 2324) {
	            this.curPos += 4;
	            continue;
	          } 
	          skip(k);
	          return true;
	      } 
	      int j = (this.buf[this.curPos + 4] & 0xFF) << 8 | this.buf[this.curPos + 5] & 0xFF;
	      if (j > 2324) {
	        this.curPos += 4;
	        continue;
	      } 
	      skip(j + 6);
	    } 
	  }
	  
	  public void close() {
	    try {
	      this.raf.close();
	    } catch (IOException iOException) {}
	  }
	  
	  private int readAudioPacket(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) throws IOException {
	    if (this.packet_data_bytes <= 0 && !audio_stream())
	      return -1; 
	    if (this.packet_data_bytes < paramInt2)
	      paramInt2 = this.packet_data_bytes; 
	    int i = 1024 - this.curPos;
	    if (paramInt2 >= i) {
	      System.arraycopy(this.buf, this.curPos, paramArrayOfbyte, paramInt1, i);
	      this.raf.read(paramArrayOfbyte, paramInt1 + i, paramInt2 - i);
	      this.raf.read(this.buf);
	      this.curPos = 0;
	    } else {
	      System.arraycopy(this.buf, this.curPos, paramArrayOfbyte, paramInt1, paramInt2);
	      this.curPos += paramInt2;
	    } 
	    needBytes(6);
	    this.packet_data_bytes -= paramInt2;
	    return paramInt2;
	  }
	  
	  public int read(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) throws IOException {
	    int i = paramInt2;
	    while (i > 0) {
	      int j;
	      if ((j = readAudioPacket(paramArrayOfbyte, paramInt1, i)) == -1) {
	        if (paramInt2 == i)
	          return -1; 
	        break;
	      } 
	      paramInt1 += j;
	      i -= j;
	    } 
	    return paramInt2 - i;
	  }
	  
	  public boolean seek(long paramLong) throws IOException {
	    return false;
	  }
	}
